import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { SheetList } from 'src/app/services/data-types/common.types';
import { SheetParams, SheetService } from 'src/app/services/sheet.service';
import { BatchActionsService } from 'src/app/store/batch-actions.service';

@Component({
  selector: 'app-sheet-list',
  templateUrl: './sheet-list.component.html',
  styleUrls: ['./sheet-list.component.less']
})
export class SheetListComponent implements OnInit {

  listParams: SheetParams = {
    cat: '全部',
    order: 'hot',
    offset: 1,
    limit: 35,
  };

  sheets!: SheetList;
  orderValue = 'hot'

  constructor(
    private route: ActivatedRoute,
    private sheetService: SheetService,
    private batchActionsServe: BatchActionsService,
    private router: Router,
  ) {
    this.listParams.cat = this.route.snapshot.queryParamMap.get('cat') || '全部';
    console.log('listParams', this.listParams)
    this.getList();
  }

  onOrderChange(order: 'hot' | 'new'){
    this.listParams.order = order
    this.listParams.offset = 1
    this.getList()
  }
  onPageChange (page: number){
    this.listParams.offset = page;
    this.getList()
  }

  ngOnInit(): void {
  }

  getList() {
    this.sheetService.getSheets(this.listParams).subscribe(sheets => this.sheets = sheets)
  }

  onPlaySheet(id:number){
    this.sheetService.playSheet(id).subscribe(list=>{
      this.batchActionsServe.selectPlayList({list, index:0})
    })
  }

  toInfo(id:number){
    this.router.navigate(['/sheetInfo',id])
  }
}
